package cn.edu.tsinghua.xkk.query;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.Text;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import cn.edu.tsinghua.xkk.entity.XKKNode;
import cn.edu.tsinghua.xkk.entity.XKKPage;
import cn.edu.tsinghua.xkk.entity.XKKPath;
import cn.edu.tsinghua.xkk.retrieval.XKKAnalizer;
import cn.edu.tsinghua.xkk.retrieval.XKKChineseSegment;

public class XKKResult{
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	int id;
	int weight;
	String url;
	String title;
	String content;
	
	public void getAttribute(int id, String query)
	{
		List<String> q = XKKChineseSegment.segment(query);
		XKKPage p = new XKKAnalizer().readXKKFile(new File(XKKPath.filepath + id));
		this.url = p.getURL();
		this.title = p.getTitle();
		Parser parser = Parser.createParser(p.getAllContent(), p.getEncoding());
		NodeIterator it;
		try {
			it = parser.elements();
			while(it.hasMoreNodes())
			{
				Node n = it.nextNode();
				dealNode(n, this, q);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void dealNode(Node n, XKKResult r, List<String>q) {
		// TODO Auto-generated method stub
		if(n instanceof Tag)
		{
			Tag tempT = (Tag)n;
			NodeList nl = tempT.getChildren();
			if(nl != null)
			{
				NodeIterator ni = nl.elements();
				try {
					while(ni.hasMoreNodes())
					{
						Node tempN = ni.nextNode();
						dealNode(tempN, r, q);
					}
				} catch (ParserException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}	
		}
		else if(n instanceof Text)
		{
			String s = n.getText();
			s = s.replaceAll("\n", " ");
			s = s.replaceAll("\r", " ");
			s = s.trim();
			
			if(s.compareTo("") != 0)
			{
				Iterator<String> it = q.iterator();
				while(it.hasNext())
				{
					String tempquery = it.next();
					int index = 0;
					if((index = s.indexOf(tempquery)) != -1)
					{
						int startIndex = Math.max(0, index - 50);
						int endIndex = Math.min(index + 50, s.length());
						String s1 = "";
						String s3 = "";
						int i = index;
						int j = index + tempquery.length();
						while(i > startIndex)
						{
							if(s.charAt(i) == '/' || s.charAt(i) == '{' ||s.charAt(i) == '}'
								|| s.charAt(i) == '[' || s.charAt(i) == ']')
							{
								return;
							}
							i--;
						}
						s1 = s.substring(i, index);
						
						while(j < endIndex)
						{
							if(s.charAt(j) == '/' || s.charAt(j) == '{' ||s.charAt(j) == '}'
								|| s.charAt(j) == '[' || s.charAt(j) == ']')
							{
								return;
							}
							j++;
						}
						
						s3 = s.substring(index + tempquery.length(), j);
						String c = s1 + tempquery + s3;
						if(!title.equals(c))
						{
							content = s1 + "<font color = \"#FF0000\">" + tempquery + "</font>" + s3;
							return;
						}
					}
				}
			}
		}
	}
	
}
